<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AuthController extends Controller
{
    use CallController;

    private $app;
    
    public function __construct(Request $request)
    {
        parent::__construct();

        $this->app = $this->factory->Auth(function ($app) use ($request){
            return $request;
        });
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    private function index()
    {
        if($this->app->ajax()){

            return $this->app->lists();
        }else{

            return view('Admin.Auth.index');
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    private function create()
    {
        if($this->app->ajax()){

            $this->app->create();

            return $this->success();
        }else{

            $auths = $this->app->normAuth();

            return view('Admin.Auth.create', ['auths' => $auths]);
        }
    }



    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    private function edit($id)
    {
        if($this->app->ajax()){

            $this->app->edit($id);

            return $this->success();
        }else{

            $auths = $this->app->normAuth();

            $res = $this->app->first($id);

            return view('Admin.Auth.edit' , ['id'=>$id , 'auths' => $auths, 'res' => $res]);
        }
    }


    private function changeStatus()
    {
        if($this->app->ajax()){

            $id =  $this->app->input('id');
            $isShow =  $this->app->input('isShow');

            $condition = ['id'=>$id];
            $data = ['is_show'=>$isShow];

            $this->app->update($condition, $data);

            return $this->success();
        }else{

            throw new \Exception('请求类型错误');
        }
    }



    private function changeSort()
    {
        if($this->app->ajax()){

            $id =  $this->app->input('id');
            $sort =  $this->app->input('sort');

            $condition = ['id'=>$id];
            $data = ['sort'=>$sort];

            $this->app->update($condition, $data);

            return $this->success();
        }else{

            throw new \Exception('请求类型错误');
        }
    }



    /**
     * Remove the specified resource from storage.
     *
     * @return \Illuminate\Http\Response
     */
    private function destroy()
    {
        if($this->app->ajax()){

            $this->app->delete();

            return $this->success();
        }else{

            throw new \Exception('请求类型错误');
        }

    }
}
